#include "CppUTest/TestHarness.h"

extern "C"
{
#include "LightScheduler.h"
#include "LightControllerSpy.h"
#include "FakeTimeService.h"
}

static void setTimeTo( int day, int minuteOfDay )
{
	FakeTimeService_SetDay( day );
	FakeTimeService_SetMinute( minuteOfDay );
}

static void checkLightState( int id, int level )
{
	LONGS_EQUAL( id, LightControllerSpy_GetLastId() );
	LONGS_EQUAL( level, LightControllerSpy_GetLastState() );
}

TEST_GROUP( LightScheduler )
{
	void setup()
	{
		LightController_Create();
		LightScheduler_Create();
	}

	void teardown()
	{
		LightController_Destroy();
		LightScheduler_Destroy();
	}
};

TEST(LightScheduler, ScheduleOnEverydayNotTimeYet)
{
	LightScheduler_ScheduleTurnOn( 3, EVERYDAY, 1200 );
	setTimeTo( MONDAY, 1199 );
	LightScheduler_Wakeup();

	checkLightState( LIGHT_ID_UNKNOWN, LIGHT_STATE_UNKNOWN );
}

TEST( LightScheduler, NoChangeToLightsDuringInitialization )
{
	checkLightState( LIGHT_ID_UNKNOWN, LIGHT_STATE_UNKNOWN );
}

TEST( LightScheduler, NoScheduleNothingHappens )
{
	setTimeTo( MONDAY, 100 );
	LightScheduler_Wakeup();
	checkLightState( LIGHT_ID_UNKNOWN, LIGHT_STATE_UNKNOWN );
}

TEST( LightScheduler, ScheduleOnEverydayItsTime )
{
	LightScheduler_ScheduleTurnOn( 3, EVERYDAY, 1200 );
	setTimeTo( MONDAY, 1200 );
	LightScheduler_Wakeup();

	checkLightState( 3, LIGHT_ON );
}

TEST( LightScheduler, ScheduleOffEverydayItsTime )
{
	LightScheduler_ScheduleTurnOff( 3, EVERYDAY, 1200 );
	setTimeTo( MONDAY, 1200 );
	LightScheduler_Wakeup();

	checkLightState( 3, LIGHT_OFF );
}

TEST( LightScheduler, ScheduleWeekEndItsMonday )
{
	LightScheduler_ScheduleTurnOn( 3, WEEKEND, 1200 );
	setTimeTo( MONDAY, 1200 );
	LightScheduler_Wakeup();

	checkLightState( LIGHT_ID_UNKNOWN, LIGHT_STATE_UNKNOWN );
}

TEST( LightScheduler, ScheduleTuesdayButItsMonday )
{
	LightScheduler_ScheduleTurnOn( 3, TUESDAY, 1200 );
	setTimeTo( MONDAY, 1200 );
	LightScheduler_Wakeup();

	checkLightState( LIGHT_ID_UNKNOWN, LIGHT_STATE_UNKNOWN );
}
